package com.zkq.graduate.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Author zhangkaiqiang
 * @Date 2019/9/28  14:52
 * @Description TODO
 */
@Component
public class CookieAndSessionUtil {


	/**
	 * 根据建从session中得到值
	 * @param request
	 * @param sessionName
	 * @return
	 */
	public static Object getSessionAttribute(HttpServletRequest request,String sessionName){
		if(request!=null&&StringUtils.isNotBlank(sessionName)){
			HttpSession session=request.getSession();
			return session.getAttribute(sessionName);
		}
		return null;
	}

	/**
	 * 移除session中的sessionName属性
	 * @param request
	 * @param sessionName
	 */
	public static void removeSessionAttribute(HttpServletRequest request, String sessionName){
		if(request!=null&&StringUtils.isNotBlank(sessionName)){
			HttpSession session=request.getSession();
			session.removeAttribute(sessionName);
		}
	}


	/**
	 * 向session中添加属性
	 * @param request
	 * @param sessionName
	 * @param sessionValue
	 */
	public  static void addAttributeToSession(HttpServletRequest request, String sessionName, Object sessionValue){
		if(request!=null&& StringUtils.isNotBlank(sessionName)&&sessionValue!=null){
			HttpSession session=request.getSession();
			session.setAttribute(sessionName,sessionValue);
		}
	}

	/**
	 * 定期清理session中的属性
	 * @param request
	 * @param sessionName
	 */
	public static void removeExpireAttributeFromSession(HttpServletRequest request, String sessionName,int time){
		// 创建一个线程池,应该手动创建的，这里使用提供的方法创建
		//ScheduledExecutorService executor = new ThreadPoolExecutor();
		ScheduledExecutorService executor= Executors.newScheduledThreadPool(2);
		executor.schedule(()->{
			HttpSession session=request.getSession();
			session.removeAttribute(sessionName);
		},time, TimeUnit.MINUTES);
	}



	/**
	 * 根据键从cookie中得到值
	 * @param request
	 * @param cookieName
	 * @return
	 */
	public static String getCookieAttribute(HttpServletRequest request,String cookieName){
		Cookie[] cookies=request.getCookies();
		String cookieValue=null;
		for(Cookie c:cookies){
			if(cookieName.equals(c.getName())){
				cookieValue= c.getValue();
				break;
			}
		}
		return cookieValue;
	}



	/**
	 * @Description TODO
	 * @Date 15:32 2019/9/27
	 * @param cookieValue cookie值
	 * @param response
	 * @return void
	 **/
	public static void addCookie(String cookieName, String cookieValue, HttpServletResponse response, int expireTime){
		if(cookieValue!=null){
			Cookie cookie=new Cookie(cookieName,cookieValue);
			//cookie的默认有效路径为当前路径的父路径,例如/loginController/checkLoginWithUserName,则当请求/loginController下的资源时才会携带cookie
			cookie.setPath("/");
			cookie.setMaxAge(expireTime);
			response.addCookie(cookie);
		}else{
			System.out.println("向cookie中添加cookie时,cookie值为null");
		}

	}


	/**
	 * 清除cookie
	 * @param request 请求
	 * @param response 响应
	 */
	public static void clearCookie(HttpServletRequest request, HttpServletResponse response,String cookieName){
		//清除cookie
		Cookie [] cookies=request.getCookies();
		for(Cookie c:cookies){
			if(cookieName.equals(c.getName())){
				c.setMaxAge(0);
				c.setPath("/");
				c.setValue(null);
				response.addCookie(c);
				break;
			}
		}
	}
}
